/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package program;

import java.text.Format;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.swing.JOptionPane;
import radiostationv41.Artist;
import radiostationv41.DBManager;
import radiostationv41.MusicGroup;

/**
 *
 * @author KiKiWi
 */
public class EditMusicGroupForm extends javax.swing.JFrame {

    MusicGroup musicGroup;
    boolean modify;
    int confirm;
   // List<Artist> artistList;
    
    /**
     * Creates new form EditMusicGroupForm
     */
    public EditMusicGroupForm() {
        initComponents();
        modify=false;
    }
    
    public EditMusicGroupForm(MusicGroup mg){
        initComponents();
        modify=true;
        musicGroup=mg;
        jTextFieldName.setText(mg.getMusicGroupName());
        Format formatter = new SimpleDateFormat("dd-MM-yyyy");
        String creationDate = formatter.format(mg.getMusicGroupFormationDate());
        jTextFieldCreationDate.setText(creationDate);
        artistList.clear();
        for(Artist art:musicGroup.getArtistList()){
            artistList.add(art);
        };
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        RadioStationV41PUEntityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("RadioStationV41PU").createEntityManager();
        artistQuery = java.beans.Beans.isDesignTime() ? null : RadioStationV41PUEntityManager.createQuery("SELECT a FROM Artist a");
        artistList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : artistQuery.getResultList();
        jLabel1 = new javax.swing.JLabel();
        jLabelName = new javax.swing.JLabel();
        jLabelCreationDate = new javax.swing.JLabel();
        jTextFieldName = new javax.swing.JTextField();
        jTextFieldCreationDate = new javax.swing.JTextField();
        jLabelListOfArtists = new javax.swing.JLabel();
        jBtnArtistIns = new javax.swing.JButton();
        jBtnArtistDelete = new javax.swing.JButton();
        jBtnSave = new javax.swing.JButton();
        jBtnCancel = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(null);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("ΦΟΡΜΑ ΔΙΑΧΕΙΡΙΣΗΣ ΣΥΓΚΡΟΤΗΜΑΤΟΣ");
        getContentPane().add(jLabel1);
        jLabel1.setBounds(50, 19, 450, 15);

        jLabelName.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabelName.setText("Επωνυμία");
        getContentPane().add(jLabelName);
        jLabelName.setBounds(200, 90, 70, 10);

        jLabelCreationDate.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabelCreationDate.setText("Ημερομηνία Δημιουργίας (ΗΗ-ΜΜ-ΕΕΕ)");
        getContentPane().add(jLabelCreationDate);
        jLabelCreationDate.setBounds(60, 120, 210, 10);
        getContentPane().add(jTextFieldName);
        jTextFieldName.setBounds(280, 80, 154, 20);

        jTextFieldCreationDate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldCreationDateActionPerformed(evt);
            }
        });
        getContentPane().add(jTextFieldCreationDate);
        jTextFieldCreationDate.setBounds(280, 110, 154, 20);

        jLabelListOfArtists.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabelListOfArtists.setText("Λίστα Καλλιτεχνών");
        getContentPane().add(jLabelListOfArtists);
        jLabelListOfArtists.setBounds(50, 174, 100, 10);

        jBtnArtistIns.setText("Εισαγωγή Καλλιτέχνη");
        jBtnArtistIns.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnArtistInsActionPerformed(evt);
            }
        });
        getContentPane().add(jBtnArtistIns);
        jBtnArtistIns.setBounds(50, 400, 135, 23);

        jBtnArtistDelete.setText("Διαγραφή Καλλιτέχνη");
        getContentPane().add(jBtnArtistDelete);
        jBtnArtistDelete.setBounds(200, 400, 137, 23);

        jBtnSave.setText("Αποθήκευση");
        jBtnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnSaveActionPerformed(evt);
            }
        });
        getContentPane().add(jBtnSave);
        jBtnSave.setBounds(380, 400, 93, 23);

        jBtnCancel.setText("Ακύρωση");
        jBtnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnCancelActionPerformed(evt);
            }
        });
        getContentPane().add(jBtnCancel);
        jBtnCancel.setBounds(490, 400, 77, 23);

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, artistList, jTable1);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${artistFirstName}"));
        columnBinding.setColumnName("Artist First Name");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${artistLastName}"));
        columnBinding.setColumnName("Artist Last Name");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${artistArtisticName}"));
        columnBinding.setColumnName("Artist Artistic Name");
        columnBinding.setColumnClass(String.class);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();

        jScrollPane1.setViewportView(jTable1);

        getContentPane().add(jScrollPane1);
        jScrollPane1.setBounds(70, 190, 452, 180);

        jScrollPane2.setViewportView(jList1);

        getContentPane().add(jScrollPane2);
        jScrollPane2.setBounds(610, 220, 170, 130);

        jButton1.setText("jButton1");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1);
        jButton1.setBounds(610, 180, 73, 23);

        jButton2.setText("jButton2");
        getContentPane().add(jButton2);
        jButton2.setBounds(700, 180, 73, 23);

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTextFieldCreationDateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldCreationDateActionPerformed
        
    }//GEN-LAST:event_jTextFieldCreationDateActionPerformed

    private void jBtnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnSaveActionPerformed
        if(jTextFieldName.getText().isEmpty()){
            JOptionPane.showMessageDialog(null,"Η επωνυμία του συγκροτήματος είναι υποχρεωτική.", "ERROR", JOptionPane.ERROR_MESSAGE);
        }else{
               if(modify==true){
               confirm = JOptionPane.showConfirmDialog(null, "Επιθυμείτε να αποθηκεύσετε τις αλλαγές;" , "",JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
               if(confirm==0){
                   musicGroup.setMusicGroupName(jTextFieldName.getText());
                   SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
                   try{
                   Date creationDate=formatter.parse(jTextFieldCreationDate.getText());
                   musicGroup.setMusicGroupFormationDate(creationDate);
                   } catch (ParseException e){
                       System.out.println(e);
                   }
                   if(DBManager.ModifyMusicGroup(musicGroup)){
                       ListMusicGroupForm.musicGroupList.set(ListMusicGroupForm.musicGroupTable.getSelectedRow(),musicGroup);
                       JOptionPane.showMessageDialog(null, "Επιτυχής τροποποίηση της λίστας τραγουδιών","SUCCESSFUL", JOptionPane.INFORMATION_MESSAGE);
                       dispose();
                    }else{
                       JOptionPane.showMessageDialog(null, "Αποτυχία τροποποίησης της λίστας τραγουδιών","ERROR", JOptionPane.ERROR_MESSAGE);
                   }
               }
            }else{
                   JOptionPane.showConfirmDialog(null, "Επιθυμείτε να ολοκληρώσετε την καταχώριση;", "",JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
                   if(confirm==0){
                        MusicGroup mg=new MusicGroup();
                        mg.setMusicGroupName(jTextFieldName.getText());
                        SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
                        try{
                        Date creationDate=formatter.parse(jTextFieldCreationDate.getText());
                        mg.setMusicGroupFormationDate(creationDate);
                        } catch (ParseException e){
                               System.out.println(e);
                         };
                        if(DBManager.SaveMusicGroup(mg)){
                            ListMusicGroupForm.musicGroupList.add(mg);
                            JOptionPane.showMessageDialog(null,"Επιτυχής καταχώριση", "SUCCESSFUL", JOptionPane.INFORMATION_MESSAGE);
                            jTextFieldName.setText("");
                            jTextFieldCreationDate.setText("");  
                        }else{
                            JOptionPane.showMessageDialog(null, "Σφάλμα επικοινωνίας με τη ΒΔ!", "ERROR", JOptionPane.ERROR_MESSAGE);
                        }
                        dispose();
                  }
                }
        }
    }//GEN-LAST:event_jBtnSaveActionPerformed

    private void jBtnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnCancelActionPerformed
        // TODO add your handling code here:
        dispose();
    }//GEN-LAST:event_jBtnCancelActionPerformed

    private void jBtnArtistInsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnArtistInsActionPerformed
        // TODO add your handling code here:
       
    }//GEN-LAST:event_jBtnArtistInsActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(EditMusicGroupForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(EditMusicGroupForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(EditMusicGroupForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(EditMusicGroupForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new EditMusicGroupForm().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.persistence.EntityManager RadioStationV41PUEntityManager;
    private java.util.List<radiostationv41.Artist> artistList;
    private javax.persistence.Query artistQuery;
    private javax.swing.JButton jBtnArtistDelete;
    private javax.swing.JButton jBtnArtistIns;
    private javax.swing.JButton jBtnCancel;
    private javax.swing.JButton jBtnSave;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabelCreationDate;
    private javax.swing.JLabel jLabelListOfArtists;
    private javax.swing.JLabel jLabelName;
    private javax.swing.JList jList1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextFieldCreationDate;
    private javax.swing.JTextField jTextFieldName;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
}
